package com.example.hp.startproject_217ybh.model;

import com.example.hp.startproject_217ybh.bean.WeatherResult;
import com.example.hp.startproject_217ybh.iface.WeatherIface;
import com.example.hp.startproject_217ybh.iface.WeatherListener;
import com.example.hp.startproject_217ybh.service.Weather51Service;
import com.example.hp.startproject_217ybh.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HP on 2017/3/14.
 */

public class Weather51Model implements WeatherIface{
    private Retrofit retrofit;
    private String BASEURL=
            "http://www.weather.51wnl.com/weatherinfo/";
   private Weather51Service weather51Service;
    public Weather51Model(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)//主机地址
                .addConverterFactory(GsonConverterFactory.create())//
                .build();
    }
    public void getWeather(String cityno, final WeatherListener listener) {
        weather51Service=
                retrofit.create(Weather51Service.class);
        Call<WeatherResult> call=
                weather51Service.getResult(cityno,1);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });


    }
}
